package  persentation.presentDepartmentDean;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Font;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JRadioButton;

import po.userPO.TeacherPO;
import po.userPO.UserPO;
import businesslogic.courseBl.CourseController;
import businesslogic.informBl.InformController;
import vo.courseVO.CourseVO;
import vo.informVO.TeacherInformVO;
import dataservice.ResultMessage;
import dataservice.userDataService.UserDatabaseService;

import java.awt.Color;
import java.rmi.Naming;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class PublishCoursePanel extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_5;
	private JTextField textField_6;
	private JButton button,button_2;
	private DepartmentdeanTabbedPane tabbedPane;
	private ButtonGroup buttonGroup;
	private JLabel label_9,label_10,label_11,label_12,label_13;
	private JComboBox comboBox,comboBox_1,comboBox2;
	private JRadioButton rdbtnNewRadioButton,radioButton,radioButton_1;
	private String depart;
	private CourseController ccController;
	private ArrayList<TeacherPO> teacherPOs;
	
	public PublishCoursePanel(String department,CourseController cc) {
		setSize(900, 690);
		setLayout(null);
		depart=department;
		ccController=cc;
		
		JLabel lblNewLabel = new JLabel("*课程编号");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel.setBounds(139, 81, 74, 15);
		add(lblNewLabel);
		
		textField = new JTextField();
		textField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		textField.setBounds(249, 77, 116, 28);
		add(textField);
		textField.setColumns(10);
		textField.addActionListener(this);
		textField.setEnabled(false);
		
		JLabel label = new JLabel("*课程名称");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label.setBounds(412, 81, 96, 15);
		add(label);
		
		textField_1 = new JTextField();
		textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		textField_1.setColumns(10);
		textField_1.setBounds(513, 74, 268, 28);
		add(textField_1);
		textField_1.addActionListener(this);
		
		JLabel label_1 = new JLabel("*课程性质");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_1.setBounds(137, 238, 76, 15);
		add(label_1);
		
		JLabel label_2 = new JLabel("*学分");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_2.setBounds(139, 157, 54, 15);
		add(label_2);
		
		JLabel label_3 = new JLabel("*学时");
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_3.setBounds(337, 158, 54, 15);
		add(label_3);
		
		JLabel label_4 = new JLabel("教师");
		label_4.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_4.setBounds(519, 158, 54, 15);
		add(label_4);
		teacherPOs=new ArrayList<TeacherPO>();
		try{
			UserDatabaseService userDataService = (UserDatabaseService)Naming.lookup("rmi://localhost:8888/userData");
			ArrayList<UserPO> findResult=userDataService.findAllByDepartment(1,department);
			
			for(UserPO user:findResult){
				TeacherPO userpo=(TeacherPO)user;
				teacherPOs.add(userpo);
			}
		}catch(Exception e){
			e.printStackTrace();
			DepartmentdeanExceptionHandler.networkException();
		}
		String[] teacher=new String[teacherPOs.size()];
		for (int i=0;i<teacherPOs.size();i++){
			teacher[i]=teacherPOs.get(i).getName();
		}
		comboBox2 = new JComboBox();
		comboBox2.setModel(new DefaultComboBoxModel(teacher));
		comboBox2.setBounds(582, 155, 132, 28);
		add(comboBox2);
		
		JLabel label_5 = new JLabel("上课时间");
		label_5.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_5.setBounds(137, 321, 76, 15);
		add(label_5);
		
		textField_5 = new JTextField();
		textField_5.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		textField_5.setColumns(10);
		textField_5.setBounds(232, 319, 187, 28);
		add(textField_5);
		
		JLabel label_6 = new JLabel("上课地点");
		label_6.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_6.setBounds(453, 322, 76, 15);
		add(label_6);
		
		textField_6 = new JTextField();
		textField_6.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		textField_6.setColumns(10);
		textField_6.setBounds(556, 319, 225, 28);
		add(textField_6);
		
		button = new JButton("确认");
		button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button.addActionListener(this);
		button.setBounds(223, 477, 93, 23);
		add(button);
		
		button_2 = new JButton("取消");
		button_2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button_2.setBounds(536, 478, 93, 23);
		button_2.addActionListener(this);
		add(button_2);
		
		rdbtnNewRadioButton = new JRadioButton("通识通修");
		rdbtnNewRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		rdbtnNewRadioButton.setBounds(244, 234, 121, 23);
		add(rdbtnNewRadioButton);
		
		radioButton = new JRadioButton("学科专业");
		radioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		radioButton.setBounds(385, 235, 121, 23);
		add(radioButton);
		
		radioButton_1 = new JRadioButton("开放选修");
		radioButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		radioButton_1.setBounds(530, 235, 121, 23);
		add(radioButton_1);
		
		buttonGroup=new ButtonGroup();
		buttonGroup.add(rdbtnNewRadioButton);
		buttonGroup.add(radioButton);
		buttonGroup.add(radioButton_1);
		
		JLabel label_7 = new JLabel("带*项为必填项");
		label_7.setForeground(Color.RED);
		label_7.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		label_7.setBounds(50, 30, 96, 15);
		add(label_7);
		
		JLabel label_8 = new JLabel("(周)");
		label_8.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_8.setBounds(450, 158, 34, 15);
		add(label_8);
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}));
		comboBox.setBounds(203, 155, 60, 21);
		add(comboBox);
		
		comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"", "0","1", "2", "3", "4", "5", "6", "7", "8","9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"}));
		comboBox_1.setBounds(385, 155, 60, 21);
		add(comboBox_1);
		
		label_9 = new JLabel("课程编号不能为空");
		label_9.setForeground(Color.RED);
		label_9.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		label_9.setBounds(202, 106, 129, 15);
		add(label_9);
		label_9.setVisible(false);
		
		label_10 = new JLabel("课程名称不能为空");
		label_10.setForeground(Color.RED);
		label_10.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		label_10.setBounds(519, 106, 119, 15);
		add(label_10);
		label_10.setVisible(false);
		
		label_11 = new JLabel("未输入学分");
		label_11.setForeground(Color.RED);
		label_11.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		label_11.setBounds(172, 187, 119, 15);
		add(label_11);
		label_11.setVisible(false);
		
		label_12 = new JLabel("未输入学时");
		label_12.setForeground(Color.RED);
		label_12.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		label_12.setBounds(373, 187, 119, 15);
		add(label_12);
		label_12.setVisible(false);
		
		label_13 = new JLabel("未选择课程性质");
		label_13.setForeground(Color.RED);
		label_13.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		label_13.setBounds(184, 263, 119, 15);
		add(label_13);
		label_13.setVisible(false);
	}
	
	public void setTabbedPane(DepartmentdeanTabbedPane tabbedPane){
		this.tabbedPane=tabbedPane;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==button){
			if (textField.getText().equals("")){
				label_9.setVisible(true);
			} else if (textField_1.getText().equals("")){
				label_10.setVisible(true);
			} else if (comboBox.getSelectedIndex()==0){
				label_11.setVisible(true);
			} else if (comboBox_1.getSelectedIndex()==0){
				label_12.setVisible(true);
			} else if (buttonGroup.getSelection()==null){
				label_13.setVisible(true);
			} else {
				int i=JOptionPane.showConfirmDialog(null,"确认发布？","确认",JOptionPane.YES_NO_OPTION);
				if(i==JOptionPane.YES_OPTION){
					ResultMessage aMessage=publishCourse();
					if (aMessage==ResultMessage.INSERT_FAILURE){
						JOptionPane.showMessageDialog(null, "课程发布失败","提示",JOptionPane.OK_OPTION);
					}else if (aMessage==ResultMessage.INSERT_SUCCESS){
						JOptionPane.showMessageDialog(null, "课程发布成功","提示",JOptionPane.INFORMATION_MESSAGE);
					} else if (aMessage==ResultMessage.INSERT_DEPLICATE_PRIMARY){
						JOptionPane.showMessageDialog(null, "存在相同课程编号的课程","提示",JOptionPane.OK_OPTION);
					} else if (aMessage==ResultMessage.INSERT_ISNULL){
						JOptionPane.showMessageDialog(null, "教师不存在","提示",JOptionPane.OK_OPTION);
					}else{
						JOptionPane.showMessageDialog(null, "课程发布失败","提示",JOptionPane.OK_OPTION);
					}
				}
			}
		}else if (e.getSource()==button_2){
			int i=JOptionPane.showConfirmDialog(null,"确认退出？","确认",JOptionPane.YES_NO_OPTION);
			if(i==JOptionPane.YES_OPTION){
				tabbedPane.removeByTitle("发布课程");
			}
		}else if (e.getSource()==textField){
			if (!textField.getText().equals("")){
				label_9.setVisible(false);
			} else {
				label_9.setVisible(true);
			}
		} else if (e.getSource()==textField_1){
			if (!textField_1.getText().equals("")){
				label_10.setVisible(false);
			} else {
				label_10.setVisible(true);
			}
		}
	}
	
	private ResultMessage publishCourse(){
		String couID=textField.getText();
		String couName=textField_1.getText();
		int credit=comboBox.getSelectedIndex();
		int coursePeriod=comboBox_1.getSelectedIndex()-1;
		String couAttriString=null;
		if (rdbtnNewRadioButton.isSelected()){
			couAttriString="通识通修";
		}
		if (radioButton.isSelected()){
			couAttriString="学科专业";
		}
		if (radioButton_1.isSelected()){
			couAttriString="开放选修";
		}
		String teaID=teacherPOs.get(comboBox2.getSelectedIndex()).getId();
		String courseTime=textField_5.getText();
		String classroom=textField_6.getText();
		if (teaID==null){
			return ResultMessage.INSERT_ISNULL;
		}
		CourseVO courseVO=new CourseVO(couID, couName, teaID, null, credit, coursePeriod, classroom, null, courseTime, couAttriString,0,0,depart , null, null);	
		ResultMessage resultMessage=null;
		try {
			resultMessage=ccController.addCourse(courseVO);
		} catch (Exception e) {
			e.printStackTrace();
			DepartmentdeanExceptionHandler.networkException();
		}
		if (resultMessage==ResultMessage.INSERT_SUCCESS){
			TeacherInformVO informVO=new TeacherInformVO(teaID, "您有一门新的课程，请添加课程信息");
			InformController i=new InformController(teaID);
			i.sendTeacherInform(informVO);
			clear();
		}
		return resultMessage;
	}
	
	public void fill(){
		CourseController controller=new CourseController(null);
		try {
			ArrayList<CourseVO> courseVOs=controller.getCourseByDepartments(depart);
			String num=Integer.toString(courseVOs.size());
			num=stringFill(num, 6, '0', true);
			String idsString="25"+num;
			textField.setText(idsString);
		} catch (Exception e) {
			e.printStackTrace();
			DepartmentdeanExceptionHandler.networkException();
		}
	}
	
	private void clear() {
		fill();
		textField_1.setText(null);
		textField_5.setText(null);
		textField_6.setText(null);
		comboBox.setSelectedIndex(0);
		comboBox_1.setSelectedIndex(0);
		rdbtnNewRadioButton.setSelected(false);
		radioButton.setSelected(false);
		radioButton_1.setSelected(false);
		comboBox2.setSelectedIndex(0);
	}
	
	/**
	 * 字符串补齐
	 * @param source 源字符串
	 * @param fillLength 补齐长度
	 * @param fillChar 补齐的字符
	 * @param isLeftFill true为左补齐，false为右补齐
	 * @return
	 */

	private static String stringFill(String source, int fillLength, char fillChar, boolean isLeftFill) {
		if (source == null || source.length() >= fillLength) return source;
		
		char[] c = new char[fillLength];
		char[] s = source.toCharArray();
		int len = s.length;
		if(isLeftFill){
			int fl = fillLength - len;
			for(int i = 0; i<fl; i++){
				c[i] = fillChar;
			}
			System.arraycopy(s, 0, c, fl, len);
		}else{
			System.arraycopy(s, 0, c, 0, len);
			for(int i = len; i<fillLength; i++){
				c[i] = fillChar;
			}
		}
		return String.valueOf(c);
	}
}
